package com.lmx.videoplay.video;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.TextureView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;
import com.lmx.videoplay.R;
import com.lmx.videoplay.VideoBean;
import com.lmx.videoplay.library.VideoPlayAdapter;
import com.lmx.videoplay.view.VideoLoadingProgressbar;

import java.util.ArrayList;
import java.util.List;

public class VideoDetailsAdapter extends VideoPlayAdapter<VideoDetailsAdapter.ViewHolder> {

    private Context mContext;
    private ViewHolder mCurrentHolder;

    public VideoPlayer videoPlayer;
    private TextureView textureView;
    public List<VideoBean> videoBeanList;
    private OnPageSelectedLister lister;

    public VideoDetailsAdapter(Context mContext, OnPageSelectedLister pageSelectedLister) {
        this.mContext = mContext;
        videoPlayer = new VideoPlayer();
        textureView = new TextureView(mContext);
        videoPlayer.setTextureView(textureView);
        videoBeanList = new ArrayList<>();
        lister = pageSelectedLister;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_video_details, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        VideoBean videoBean = videoBeanList.get(position);
        RequestOptions options = new RequestOptions().diskCacheStrategy(DiskCacheStrategy.RESOURCE);
        Glide.with(mContext)
                .load(videoBean.getVideoImg())
                .apply(options)
                .into(holder.ivCover);
    }

    @Override
    public int getItemCount() {
        return videoBeanList.size();
    }

    public void setData(List<VideoBean> videoBeans){
        if (this.videoBeanList != null) {
            if (this.videoBeanList.size() > 0) {
                this.videoBeanList.clear();
            }
        }
        this.videoBeanList.addAll(videoBeans);
        this.notifyDataSetChanged();
    }

    @Override
    public void onPageSelected(int itemPosition, View itemView) {
        mCurrentHolder = new ViewHolder(itemView);
        playVideo(itemPosition);
    }

    private void playVideo(int itemPosition) {
        if (lister != null) {
            lister.onPageSelected(itemPosition);
        }
        VideoBean videoBean = videoBeanList.get(itemPosition);
        videoPlayer.reset();
        mCurrentHolder.pbLoading.setVisibility(View.VISIBLE);
        videoPlayer.setOnStateChangeListener(new VideoPlayer.OnStateChangeListener() {
            @Override
            public void onReset() {
                mCurrentHolder.ivCover.setVisibility(View.VISIBLE);
                mCurrentHolder.pbLoading.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onRenderingStart() {
                mCurrentHolder.ivCover.setVisibility(View.GONE);
                mCurrentHolder.pbLoading.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onProgressUpdate(float per) {
            }

            @Override
            public void onPause() {
                mCurrentHolder.pbLoading.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onStop() {
                mCurrentHolder.ivCover.setVisibility(View.VISIBLE);
                mCurrentHolder.pbLoading.setVisibility(View.INVISIBLE);
            }

            @Override
            public void onComplete() {
                videoPlayer.start();
            }
        });
        if (textureView.getParent() != mCurrentHolder.flVideo) {
            if (textureView.getParent() != null) {
                ((FrameLayout) textureView.getParent()).removeView(textureView);
            }
            mCurrentHolder.flVideo.addView(textureView);
        }
        videoPlayer.setDataSource(videoBean.getVideoUrl());
        videoPlayer.prepare();
    }

    public void release() {
        videoPlayer.release();
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        private FrameLayout flVideo;
        private ImageView ivCover;
        private VideoLoadingProgressbar pbLoading;

        ViewHolder(@NonNull View itemView) {
            super(itemView);
            flVideo = itemView.findViewById(R.id.flVideo);
            ivCover = itemView.findViewById(R.id.ivCover);
            pbLoading = itemView.findViewById(R.id.pbLoading);
        }
    }

    public interface OnPageSelectedLister {
        void onPageSelected(int position);
    }

}
